package com.victor.controller.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.victor.entity.Problem;
import com.victor.service.ProblemService;
import com.victor.utils.FileUpload;
import com.victor.utils.Msg;

/**
 * 问题管理Controller
 * 
 * @author zhangbin
 *
 */
@RestController
@RequestMapping("/problem")
public class ProblemController {

	@Autowired
	private ProblemService problemService;

	// 获取问题列表（传类型）type 1.问答题,2.选择题,3.连线题,4.拼图题
	@RequestMapping("/getAllProblem")
	public Msg getAllProblem(String type) {
		List<Problem> problems = problemService.getAllProblem(type);
		return Msg.success().add("problems", problems);
	}

	// 新建问题 所有文件类型为file
	@RequestMapping("/createNewProblem")
	public Msg createNewProblem(Problem problem, MultipartFile audio_topic_file, MultipartFile problemphoto_url_file, MultipartFile answer_A_file, MultipartFile answer_B_file, MultipartFile answer_C_file, MultipartFile answer_D_file, MultipartFile answer_right_file, MultipartFile line_photo1_file, MultipartFile line_photo2_file, MultipartFile line_photo3_file, MultipartFile line_photo4_file, MultipartFile line_photo5_file, MultipartFile line_photo6_file, MultipartFile line_photo7_file, MultipartFile line_photo8_file, HttpServletRequest request) throws Exception {
		// 音频题干
		if (audio_topic_file != null) {
			problem.setAudio_topic(FileUpload.uploadFile(audio_topic_file, request, "audio"));
		}
		// 问题图片地址
		if (problemphoto_url_file != null) {
			problem.setProblemphoto_url(FileUpload.uploadFile(problemphoto_url_file, request, "photo"));
		}
		// 答案A&图片答案1
		if (answer_A_file != null) {
			problem.setAnswer_A(FileUpload.uploadFile(answer_A_file, request, "photo"));
		}
		// 答案B&图片答案2
		if (answer_B_file != null) {
			problem.setAnswer_B(FileUpload.uploadFile(answer_B_file, request, "photo"));
		}
		// 答案C&图片答案3
		if (answer_C_file != null) {
			problem.setAnswer_C(FileUpload.uploadFile(answer_C_file, request, "photo"));
		}
		// 答案D&图片答案4
		if (answer_D_file != null) {
			problem.setAnswer_D(FileUpload.uploadFile(answer_D_file, request, "photo"));
		}
		// 正确答案
		if (answer_right_file != null) {
			problem.setAnswer_right(FileUpload.uploadFile(answer_right_file, request, "photo"));
		}
		// 连线题图片1
		if (line_photo1_file != null) {
			problem.setLine_photo1(FileUpload.uploadFile(line_photo1_file, request, "photo"));
		}
		// 连线题图片2
		if (line_photo2_file != null) {
			problem.setLine_photo2(FileUpload.uploadFile(line_photo2_file, request, "photo"));
		}
		// 连线题图片3
		if (line_photo3_file != null) {
			problem.setLine_photo3(FileUpload.uploadFile(line_photo3_file, request, "photo"));
		}
		// 连线题图片4
		if (line_photo4_file != null) {
			problem.setLine_photo4(FileUpload.uploadFile(line_photo4_file, request, "photo"));
		}
		// 连线题图片5
		if (line_photo5_file != null) {
			problem.setLine_photo5(FileUpload.uploadFile(line_photo5_file, request, "photo"));
		}
		// 连线题图片6
		if (line_photo6_file != null) {
			problem.setLine_photo6(FileUpload.uploadFile(line_photo6_file, request, "photo"));
		}
		// 连线题图片7
		if (line_photo7_file != null) {
			problem.setLine_photo7(FileUpload.uploadFile(line_photo7_file, request, "photo"));
		}
		// 连线题图片8
		if (line_photo8_file != null) {
			problem.setLine_photo8(FileUpload.uploadFile(line_photo8_file, request, "photo"));
		}

		boolean insResult = problemService.createNewProblem(problem);
		if (insResult) {
			return Msg.success();
		}
		return Msg.fail();
	}

	// 批量删除问题
	@RequestMapping("/batchDeleteProblem")
	public Msg batchDeleteProblem(int[] ids) {
		boolean delResult = problemService.batchDeleteProblem(ids);
		if (delResult) {
			return Msg.success();
		}
		return Msg.fail();
	}

	// 修改问题信息
	@RequestMapping("/updateProblem")
	public Msg updateProblem(Problem problem, MultipartFile audio_topic_file, MultipartFile problemphoto_url_file, MultipartFile answer_A_file, MultipartFile answer_B_file, MultipartFile answer_C_file, MultipartFile answer_D_file, MultipartFile answer_right_file, MultipartFile line_photo1_file, MultipartFile line_photo2_file, MultipartFile line_photo3_file, MultipartFile line_photo4_file, MultipartFile line_photo5_file, MultipartFile line_photo6_file, MultipartFile line_photo7_file, MultipartFile line_photo8_file, HttpServletRequest request) throws Exception {
		// 如果传入id为0 则id未传入
		if (problem.getId() == 0) {
			return Msg.fail("参数错误");
		}

		// 音频题干
		if (audio_topic_file != null) {
			problem.setAudio_topic(FileUpload.uploadFile(audio_topic_file, request, "audio"));
		}
		// 问题图片地址
		if (problemphoto_url_file != null) {
			problem.setProblemphoto_url(FileUpload.uploadFile(problemphoto_url_file, request, "photo"));
		}
		// 答案A&图片答案1
		if (answer_A_file != null) {
			problem.setAnswer_A(FileUpload.uploadFile(answer_A_file, request, "photo"));
		}
		// 答案B&图片答案2
		if (answer_B_file != null) {
			problem.setAnswer_B(FileUpload.uploadFile(answer_B_file, request, "photo"));
		}
		// 答案C&图片答案3
		if (answer_C_file != null) {
			problem.setAnswer_C(FileUpload.uploadFile(answer_C_file, request, "photo"));
		}
		// 答案D&图片答案4
		if (answer_D_file != null) {
			problem.setAnswer_D(FileUpload.uploadFile(answer_D_file, request, "photo"));
		}
		// 正确答案
		if (answer_right_file != null) {
			problem.setAnswer_right(FileUpload.uploadFile(answer_right_file, request, "photo"));
		}
		// 连线题图片1
		if (line_photo1_file != null) {
			problem.setLine_photo1(FileUpload.uploadFile(line_photo1_file, request, "photo"));
		}
		// 连线题图片2
		if (line_photo2_file != null) {
			problem.setLine_photo2(FileUpload.uploadFile(line_photo2_file, request, "photo"));
		}
		// 连线题图片3
		if (line_photo3_file != null) {
			problem.setLine_photo3(FileUpload.uploadFile(line_photo3_file, request, "photo"));
		}
		// 连线题图片4
		if (line_photo4_file != null) {
			problem.setLine_photo4(FileUpload.uploadFile(line_photo4_file, request, "photo"));
		}
		// 连线题图片5
		if (line_photo5_file != null) {
			problem.setLine_photo5(FileUpload.uploadFile(line_photo5_file, request, "photo"));
		}
		// 连线题图片6
		if (line_photo6_file != null) {
			problem.setLine_photo6(FileUpload.uploadFile(line_photo6_file, request, "photo"));
		}
		// 连线题图片7
		if (line_photo7_file != null) {
			problem.setLine_photo7(FileUpload.uploadFile(line_photo7_file, request, "photo"));
		}
		// 连线题图片8
		if (line_photo8_file != null) {
			problem.setLine_photo8(FileUpload.uploadFile(line_photo8_file, request, "photo"));
		}

		boolean updResult = problemService.updateProblem(problem);
		if (updResult) {
			return Msg.success();
		}
		return Msg.fail();
	}
}
